package com.sms;

import com.alibaba.fastjson.JSONObject;
import com.base.BaseException;
import com.http.HttpUtils;

import java.util.HashMap;
import java.util.Map;

public class SmsUtils {

    private static String url = "http://211.90.31.47:8004/api/SMSApi";
    private static String mxtUserId = "996568";
    private static String mxtAccount = "jhqy2023";
    private static String mxtPassWord = "jhqy2023";
    private static String LabelText = "福运河北";
    private static String success = "Yes";
    private static String status = "status";


    private static String codeContent = "验证码{code}，您正在登录，若非本人操作，请勿泄露。";
    public static void sendCode(String phone, String code) {
        String conetnt = codeContent.replace("{code}", code);
        Map<String, Object> param = new HashMap<>();
        param.put("mxtUserId", mxtUserId);
        param.put("mxtAccount", mxtAccount);
        param.put("mxtPassWord", mxtPassWord);
        param.put("Phone", phone);
        param.put("smsContent", conetnt);
        param.put("LabelText", LabelText);
        String resultText = HttpUtils.httpPost(url, param);
        resultText = resultText.replace("\\", "");
        resultText = resultText.substring(1, resultText.length() - 1);
        JSONObject jsonObject = JSONObject.parseObject(resultText);
        if (!jsonObject.getString(status).equals(success)) {
            throw new BaseException("发送短信失败，请稍后尝试！");
        }
    }

    private static String totalWarnContent = "活动[{activityName}]立减金领取总量已达到{warnsRate}%，请您尽快处理";
    public static void sendTotalWarn(String phone, String activityName, String warnRate) {
        String conetnt = totalWarnContent;
        conetnt = conetnt.replace("{activityName}", activityName).replace("{warnsRate}", warnRate);
        Map<String, Object> param = new HashMap<>();
        param.put("mxtUserId", mxtUserId);
        param.put("mxtAccount", mxtAccount);
        param.put("mxtPassWord", mxtPassWord);
        param.put("Phone", phone);
        param.put("smsContent", conetnt);
        param.put("LabelText", LabelText);
        String resultText = HttpUtils.httpPost(url, param);
        resultText = resultText.replace("\\", "");
        resultText = resultText.substring(1, resultText.length() - 1);
        JSONObject jsonObject = JSONObject.parseObject(resultText);
        if (!jsonObject.getString(status).equals(success)) {
            throw new BaseException("发送短信失败，请稍后尝试！");
        }
    }

    private static String todayWarnContent = "活动[{activityName}]当日立减金领取额度已达到{warnsRate}%，请知晓";
    public static void sendTodayWarn(String phone, String activityName, String warnRate) {
        String conetnt = todayWarnContent;
        conetnt = conetnt.replace("{activityName}", activityName).replace("{warnsRate}", warnRate);
        Map<String, Object> param = new HashMap<>();
        param.put("mxtUserId", mxtUserId);
        param.put("mxtAccount", mxtAccount);
        param.put("mxtPassWord", mxtPassWord);
        param.put("Phone", phone);
        param.put("smsContent", conetnt);
        param.put("LabelText", LabelText);
        String resultText = HttpUtils.httpPost(url, param);
        resultText = resultText.replace("\\", "");
        resultText = resultText.substring(1, resultText.length() - 1);
        JSONObject jsonObject = JSONObject.parseObject(resultText);
        if (!jsonObject.getString(status).equals(success)) {
            throw new BaseException("发送短信失败，请稍后尝试！");
        }
    }

}

